package com.dcjt.firehotcarpad.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.drawable.AnimationDrawable
import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders
import com.aries.library.fast.basis.BasisFragment
import com.aries.library.fast.util.ToastUtil
import com.aries.ui.view.radius.RadiusRelativeLayout
import com.aries.ui.view.radius.RadiusTextView
import com.aries.ui.widget.progress.UIProgressView
import com.dcjt.firecommand.manager.MapDataRec
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.entity.Zqxx
import com.dcjt.firehotcarpad.manager.DataManager
import com.dcjt.firehotcarpad.manager.MediaManager
import com.dcjt.firehotcarpad.model.HandleDisasterViewModel
import com.dcjt.firehotcarpad.utils.AlertDialogUtil
import com.dcjt.firehotcarpad.utils.AlertDialogUtil.showTipAlert
import com.dcjt.firehotcarpad.utils.StringUtils
import java.util.*


class DisasterInfoFragment : BasisFragment() {

    private var type: Int = 1
    private var absfile: String = ""
    private var dialog: UIProgressView? = null
    private var iv_ajlx: ImageView? = null
    private var tv_name: TextView? = null
    private var tv_address: TextView? = null
    private var tv_time: TextView? = null
    private var tv_jigou: TextView? = null
    private var tv_phone: TextView? = null
    private var rtv_zt: RadiusTextView? = null
    private var imageView: ImageView? = null
    private var relativeLayout: RadiusRelativeLayout? = null
    private var preVideoPos = -1

    private var mAnimationDrawables: ArrayList<AnimationDrawable> = ArrayList()

    companion object {
        fun newInstance(): DisasterInfoFragment {
            return DisasterInfoFragment()
        }
    }

    override fun getContentLayout(): Int = R.layout.fragment_disaster_info
    override fun initView(savedInstanceState: Bundle?) {

        iv_ajlx = findViewById<ImageView>(R.id.iv_icon)
        tv_name = findViewById<TextView>(R.id.tv_name)
        tv_address = findViewById<TextView>(R.id.tv_address)
        tv_time = findViewById<TextView>(R.id.tv_time)
        tv_jigou = findViewById<TextView>(R.id.tv_jigou)
        tv_phone = findViewById<TextView>(R.id.tv_phone)
        rtv_zt = findViewById<RadiusTextView>(R.id.rtv_zt)
        tv_phone?.setOnClickListener {
            //拨打电话号码
            showTipAlert(tv_phone!!.text.trim().toString() )
        }
        relativeLayout = findViewById<RadiusRelativeLayout>(R.id.relativeLayout)
        relativeLayout?.setOnClickListener {
            //网络文件
            if (!StringUtils.isEmpty(absfile)) {
                if (DataManager.getInstance().appConfigEntity != null) {
                    imageView = findViewById<ImageView>(R.id.message_item_audio_playing_animation)

                    val animation =
                        imageView?.background as AnimationDrawable
                    resetAnim(animation)

                    if (MediaManager.isPlaying()) {
                        MediaManager.release()
                        animation.stop()
                        animation.selectDrawable(0)
                        return@setOnClickListener
                    }


                    animation.start()
                    MediaManager.playSound(
                        DataManager.getInstance().appConfigEntity.appServerUrl.recordUrl + absfile
//                        "http://182.150.35.222:8813/new_bigdisaster.wav"
                    ) { mediaPlayer: MediaPlayer? ->
                        animation.stop()
                        animation.selectDrawable(0)
                        preVideoPos = -1
                    }
                }
            } else {
                ToastUtil.show("文件地址出错")
            }
        }

        val handleViewModel =
            ViewModelProviders.of(requireActivity()).get(HandleDisasterViewModel::class.java)
        handleViewModel.getDisasterInfo().observe(this, androidx.lifecycle.Observer { entity ->
            entity.zqxx?.let {
                absfile = it.lyxx.toString()
                disasterDetail(it)
            }
        })

    }

    private fun resetAnim(animationDrawable: AnimationDrawable) {
        if (!mAnimationDrawables.contains(animationDrawable)) {
            mAnimationDrawables.add(animationDrawable)
        }
        for (ad in mAnimationDrawables) {
            ad.selectDrawable(0)
            ad.stop()
        }
    }



    @SuppressLint("UseRequireInsteadOfGet")
    override fun beforeInitView() {
//        type = arguments!!.getInt("type")
        super.beforeInitView()
    }

    private fun disasterDetail(disasterEntity: Zqxx) {
        iv_ajlx!!.setImageResource(MapDataRec.getCaseIcon(disasterEntity.ajlx))
        if(!StringUtils.isEmpty(disasterEntity!!.xzqmc))
        tv_name!!.text =
              "${disasterEntity!!.xzqmc}${disasterEntity!!.ajdjmc}${disasterEntity!!.ajlxmc}"
        rtv_zt!!.text = disasterEntity?.ajztmc
        tv_address!!.text = disasterEntity?.sfdz
        tv_time!!.text = disasterEntity?.lasj
        tv_jigou!!.text = disasterEntity?.zgdw
        tv_phone!!.text = disasterEntity?.lxdh
        if (CarStatePool.get().getCurrentZqidPage() == disasterEntity.id){
            relativeLayout!!.visibility = if (StringUtils.isEmpty(disasterEntity.lyxx)) View.GONE else View.VISIBLE
        }

    }

}
